package com.alex.j2se.bigDecimal;

import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Author  : gaojun            <br/>
 * Date    : 2020/8/11                          <br/>
 * Time    : 10:29 上午                         <br/>
 * ---------------------------------------    <br/>
 * Desc    :
 */
@Slf4j
public class BigDecimalTest {
    public static void main(String[] args) {
        System.out.println(String.format("￥%s", new DecimalFormat("0.00").format(new BigDecimal(61880).divide(new BigDecimal(100)))));
    }

    @Test
    public void compareFromDoubleValFalse() {
        BigDecimal bigDecimal = new BigDecimal(0.4);
        BigDecimal val = new BigDecimal(0.5).subtract(new BigDecimal(0.1));
        System.out.println("bigDecimal:" + bigDecimal + " val:" + val);
        Assert.assertFalse(bigDecimal.equals(val));
    }

    @Test
    public void compareFromDoubleValTrue1() {
        BigDecimal bigDecimal = new BigDecimal("0.4");
        BigDecimal val = new BigDecimal("0.5").subtract(new BigDecimal("0.1"));
        System.out.println("bigDecimal:" + bigDecimal + " val:" + val);
        Assert.assertTrue(bigDecimal.equals(val));
    }

    @Test
    public void compareFromDoubleValTrue2() {
        BigDecimal bigDecimal = BigDecimal.valueOf(0.4);
        BigDecimal val = BigDecimal.valueOf(0.5).subtract(BigDecimal.valueOf(0.1));
        System.out.println("bigDecimal:" + bigDecimal + " val:" + val);
        Assert.assertTrue(bigDecimal.equals(val));
    }

    @Test
    public void String2BigDecimal() {

        BigDecimal b1 = new BigDecimal("21.10");
        BigDecimal b2 = new BigDecimal("1.10");

        System.out.println(b1);
    }

    @Test
    public void compare() {
        Assert.assertTrue(new BigDecimal(1).compareTo(BigDecimal.ZERO) == 1);
        Assert.assertTrue(new BigDecimal(0).compareTo(BigDecimal.ZERO) == 0);
        Assert.assertTrue(new BigDecimal(-1).compareTo(BigDecimal.ZERO) == -1);
    }

    @Test
    public void logNull() {
        BigDecimal nullObj = null;
        log.info("nullObj:{}", nullObj);
    }

    @Test
    public void movePointLeftTest() {
        Assert.assertEquals(new BigDecimal(String.valueOf("12.99")), new BigDecimal(1299).movePointLeft(2));
    }
}
